<?php
// +----------------------------------------------------------------------
// | SL 2018.8.17
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2018 http://sl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zq <1040657944@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\model\RoleModel;
use app\admin\model\UserModel;
use think\Controller;

class User extends Base
{
    /**
     * 管理员列表
    */
    public function index()
    {
        if(request()->isAjax()){
            $role = new RoleModel();
            $param = input("param.");
            $where = [];
            $where['role_id'] = ['neq',1];
            $limit = $param['limit'];
            $offset = ($param['page'] - 1) * $limit;
            $user = new UserModel();
            $user_list = $user->getAllUsers($where,$offset,$limit);
            foreach($user_list as $key=>$vo){
                $role_info = $role->getOneRole(['role_id' => $vo['role_id']]);
                $user_list[$key]['role_id'] = $role_info['role_name'];
                $user_list[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }

            $return['count'] = $user->getAllUsersCount($where);  //总数据
            $return['code'] = "0"; //状态码
            $return['data'] = $user_list;
            return json($return);
        }
        return $this->fetch();
    }

    /**
     * 添加管理员
    */
    public function addUser()
    {
        if(request()->isPost()){
            $param = input("post.");
            $user = new UserModel();
            $flag = $user->insertUser($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $role = new RoleModel();
        $role_list = $role->getAllRole();
        $this->assign([
            'role_list' => $role_list
        ]);
        return $this->fetch();
    }

    /**
     * 编辑管理员
    */
    public function editUser()
    {
        $user = new UserModel();
        $role = new RoleModel();
        if(request()->isPost()){
            $param = input("post.");
            $flag = $user->editUser($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input("param.id");
        $user_info = $user->getOneUser(['id'=>$id]);
        // var_dump($user_info);exit;
        $this->assign([
            'user_info' => $user_info,
            'role_list' => $role->getAllRole()
        ]);
        return $this->fetch();
    }

    /**
     * 删除数据
    */
    public function userDel()
    {
        $id = input("param.id");
        $user = new UserModel();
        $flag = $user->delUser($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'user/edituser',
                'href' => url('user/edituser', ['id' => $id]),
                'btnStyle' => 'normal',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'user/userdel',
                'href' => "javascript:userDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];
    }

}